package com.lzy.example.controller;

import com.lzy.example.entity.User;
import com.lzy.example.repo.UserRepositoryJpa;
import com.lzy.example.utils.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@Api(tags = "用户控制器", description = "对用户进行增删查改等操作")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserRepositoryJpa userRepositoryJpa;

    @ApiOperation(value = "添加用户", notes = "根据实体添加用户")
    @PostMapping("/addUser")
    public User addUser(User user) {
        return userRepositoryJpa.save(user);
    }


    @GetMapping("/findById")
    public ResultBean findById(Integer id) {
        Optional<User> o = userRepositoryJpa.findById(id);
        if (o.isPresent()) {
            User user = o.get();
            return ResultBean.success(user);
        } else {
            return ResultBean.successCode(205, "用户不存在");
        }

    }
}
